import json
from django.http import HttpResponse
from django.core.cache import cache
from datetime import date, datetime,timedelta
from booking.models import Orders

def adminwork(request):
    data = json.loads(request.body)
    rets = {}
    rets['ret'] = 'F2'
    if data['power'] >= 3:
        openid = data['openid']
        worknums = cache.get('worknums')
        mylines = []
        for i in worknums:
            ans = cache.get(i)
            dc = {}
            dc['uid'] = i
            dc['nums'] = ans[0]
            dc['show'] = True
            mylines.append(dc)
        linenums = cache.get('linenums')
        lines = []
        for i in linenums:
            ans = cache.get(i)
            dc = {}
            dc['uid'] = i
            dc['nums'] = ans[0]
            dc['st'] = True
            lines.append(dc)
        rets['ret'] = 'F1'
        rets['mylines'] = mylines
        rets['lines'] = lines
        return HttpResponse(json.dumps(rets))
    return HttpResponse(json.dumps(rets))

def gowork(request):
    data = json.loads(request.body)
    ot = data['other']
    li = 'linenums'
    wk = 'worknums'
    linenums = cache.get(li)
    worknums = cache.get(wk)
    if ot in linenums:
        linenums.pop(linenums.index(ot))
        worknums.append(ot)
        _ = cache.get(ot)
        _[2] = 4
        cache.set(li,linenums,24*60*60)
        cache.set(wk,worknums,24*60*60)
        cache.set(ot,_,24*60*60)
        return HttpResponse(json.dumps({'ret':'F1'}))
    return HttpResponse(json.dumps({'ret':'F2'}))


def over(request):
    data = json.loads(request.body)
    ot = data['other']
    myid = data['openid']
    wk = 'worknums' 
    worknums = cache.get(wk)
    if ot in worknums:
        _ = worknums.index(ot)
        worknums.pop(_)
        Orders.objects.create(uid=ot,wid=myid,times=datetime.now())
        cache.set(wk,worknums,24*60*60)
        cache.set(ot,[],1)
        return HttpResponse(json.dumps({'ret':'F1'}))
    return HttpResponse(json.dumps({'ret':'F2'}))


